﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;
using System.Text;

using CE.User;
using CE.Global;
using CE.Database;
using CE.InputOut;
using CE.Mail;

public partial class carinsurance_offline_FollowUpPolicy : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole].ToString() == "100")
        {
            Response.Redirect("~/SiteUserHome.aspx");
        }

       if (!IsPostBack)
       {
           GetFollowUpPilocy();
       }
       else
       {
           lblError.Text = "";
           lblMessage.Text = "";
       }
    }

    private void GetFollowUpPilocy()
    {
        try 
	    {	        
    		MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
            string _spName = "[sprocCXCRMNonGGFollowUpPolicy]";

            DataTable _dtSummary = _db.GetDataTableBySP(_spName);
            grvApplication.DataSource = _dtSummary;
            grvApplication.DataBind();
	    }
	    catch 
	    {}
    }

    protected void grvApplication_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvApplication.SelectedIndex >= 0)
        {
            DataKey _dataKey = grvApplication.SelectedDataKey;
            string Email = _dataKey[1].ToString();
            string LicensePlate = _dataKey[2].ToString();

            grvApplication.SelectedRow.BackColor = System.Drawing.Color.PaleVioletRed;
        }
    }

    protected void grvApplication_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("MyEmail"))
        {
            GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int rowIndex = gvr.RowIndex;
            grvApplication.SelectedIndex = rowIndex;
            GridViewRow row = grvApplication.SelectedRow;

            string Email = grvApplication.SelectedDataKey[1].ToString();
            string LicensePlate = grvApplication.SelectedDataKey[2].ToString();
            string CECarMoreDetailId = grvApplication.SelectedDataKey[3].ToString();
            CheckBox chkEN = (CheckBox) row.FindControl("chkEng");

            if (SendMailingStatusToCustomer(row.Cells[2].Text, Email, LicensePlate, chkEN.Checked))
            {
                MyDatabase _db = new MyDatabase();
                string strUpdate = "Update tblNonGGCarMoreDetails SET Dinfo1 = dbo.fn_getUTCDateTime() Where CECarMoreDetailId = @CECarMoreDetailId";
                SortedList _paraList = new SortedList();
                _paraList.Add("@CECarMoreDetailId", CECarMoreDetailId);
                int _affRow = _db.ExecuteNonSqlStatement(strUpdate, _paraList);
                lblMessage.Text = "Email sent sucessfully.";
            }
            else
            {
                lblError.Text = "Error sending email.";
            }
        }
        if (e.CommandName.Equals("MyRemark"))
        {
            GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int rowIndex = gvr.RowIndex;
            grvApplication.SelectedIndex = rowIndex;
            GridViewRow row = grvApplication.SelectedRow;

            string CECarMoreDetailId = grvApplication.SelectedDataKey[3].ToString();
            TextBox txtRemark = (TextBox)row.FindControl("txtRemark");

            MyDatabase _db = new MyDatabase();
            string strUpdate = "Update tblNonGGCarMoreDetails SET info12 = @Remark Where CECarMoreDetailId = @CECarMoreDetailId";
            SortedList _paraList = new SortedList();
            _paraList.Add("@CECarMoreDetailId", CECarMoreDetailId);
            _paraList.Add("@Remark", txtRemark.Text.Trim());
            int _affRow = _db.ExecuteNonSqlStatement(strUpdate, _paraList);
            
            if (_affRow > 0)
            {
                lblMessage.Text = "Saved successfully.";
            }
            else
            {
                lblError.Text = "Cannot save remark.";
            }
        }
    }

    private bool SendMailingStatusToCustomer(string Name, string Email, string LicenseNumber, bool IsEN)
    {
        bool _retValue = false;

        string _MSMailBody, _MSEmailTitle, _MSToEmail, _MSCCEmail, _MSBCCEmail;
        _MSMailBody = ""; _MSEmailTitle = ""; _MSToEmail = ""; _MSCCEmail = ""; _MSBCCEmail = "";

        string imgUrl = "http://www.gluaygluay.com/"; //CE.Configuration.MyConfigurationManager.GetAppSettingValue("CRMPath") + "/";

        StringBuilder strMailBody = new StringBuilder();
        strMailBody.Append(@"<html>
                            <head>
                            <meta charset='utf-8'>
                            <title>GluayGluay</title>
                            <style>
                            body {
	                            margin-left: 0px;
	                            margin-top: 0px;
	                            margin-right: 0px;
	                            margin-bottom: 0px;
	                            font-family: tahoma, Arial, sans-serif;
	                            font-size: 14px;
	                            color: #4F4356 ;
	                            background-repeat: repeat-x;
	                            background-color: #fff;
	                            text-decoration: none;
                            }
                            a:link  {text-decoration:none; color:#0674DB;}
                            hr{display: block; height: 1px;
                                border: 0; border-top: 1px solid #ccc;
                                margin: 1em 0; padding: 0;}
	                            .line-height{line-height:20px;}
	                            .table-txt{padding-left:5px;}
                            </style>
                            </head><body>
                            <div class='email-containter'>
                            <div class='email-banner'></div>
                            <div class='email-text'>
                            <table width='800' border='0' align='center' cellpadding='0' cellspacing='0'>
                            <tr>
                            <td height='653' align='right' valign='bottom' bgcolor='#FFFFFF'>
                            <table width='800' height='100' border='0' cellspacing='0' cellpadding='0'>");
        strMailBody.Append("<tr><td><span class='email-banner'><img src='" + imgUrl + "images/email-header.jpg' width='800' height='100' alt=''/></span></td>");
        strMailBody.Append(@"</tr>
                            </table>
                            <table width='800' border='0' align='center' cellpadding='0' cellspacing='0'>
                            <tr>
                                <td width='672' align='left' valign='top' bgcolor='#FFFFFF'>
                            <table border='0' cellspacing='0' cellpadding='0'>
                            <tr>
                            <td align='left'>");
        _MSMailBody = strMailBody.ToString();
        if (!IsEN)
        {
            _MSEmailTitle = "สถานะการจัดส่งกรมธรรม์ของ ทะเบียนรถ [" + LicenseNumber + "]";
            _MSMailBody += "<p>เรียน  " + Name + ",<br/></p>";
            _MSMailBody += "<p>ตามที่ท่านได้แจ้งต่อประกันภัยรถยนต์ ทะเบียน " + LicenseNumber;
            _MSMailBody += " ผ่านทางโบรกเกอร์ประกันภัย คอมแพร์เอ็กซ์เพรสนายหน้าประกันภัย (ประเทศไทย)จำกัด หรือ GluayGluay.com นั้น";
            _MSMailBody += "เนื่องจาก บริษัทฯ ประกันภัย ยังมิได้นำส่งเอกสารกรมธรรม์ของท่าน มายังบริษัทฯ เรากำลังดำเนินการติดตาม กรมธรรม์ประกันภัยรถยนต์ ของท่านอย่างเร่งด่วน";
            _MSMailBody += "และหากติดขัดเรื่องเอกสารใด ๆ ทางบริษัทฯ ขออนุญาติติดต่อกลับไปยังท่านอีกครั้งเพื่อแจ้งรายละเอียดต่อไป</p> ";
            _MSMailBody += "<p>หรือถ้ามีข้อสงสัยประการใด กรุณาติดต่อเรา อีเมลล์ support@gluaygluay.com หรือ โทรหาเราที่เบอร์ <strong>02-2688222</strong></p>";

            _MSMailBody += "</td></tr><tr><td align=\"left\">";
            _MSMailBody += "ขอแสดงความนับถือ</td></tr></table><hr><p>" +
                                "<br/><strong>ฝ่ายประสานงาน </strong> <a href='http://www.gluaygluay.com'>GluayGluay</a><br/>" +           
                                "บริษัท คอมแพร์เอ็กซ์เพรส นายหน้าประกันภัย (ประเทศไทย) จำกัด<br/>" +
                                "เลขทะเบียนนายหน้าประกันวินาศภัยนิติบุคคล เลขที่ ว00016/2553<br/>" +
                                "ชั้น 42 อาคารยูไนเต็ด เซ็นเตอร์<br/>" +
                                "เลขที่ 323 ถนนสีลม แขวงสีลม<br/>" +
                                "เขตบางรัก กรุงเทพฯ 10500</p>" +
                                "<p>โทร. 02-268-8222 <br>โทรสาร 02-266-6339<br/>" +
                                "<a href=\"mailto:support@gluaygluay.com\">support@gluaygluay.com</a></p></td>" +
                                "</tr></table><hr>";
        }
        else
        {
            _MSEmailTitle = "Policy sending status for [" + LicenseNumber + "]";
            _MSMailBody += "<p>Dear " + Name + ",<br/></p>";
            _MSMailBody += "<p>We would like to update you on the car insurance (Car plate " + LicenseNumber + ") with Comparexpress Insurance Broker (Thailand) Ltd or GluayGluay.com, ";
            _MSMailBody += "we have yet to receive the printed policy from the insurance company. Our support team will contact the insurer to ";
            _MSMailBody += "follow up on this matter and we will get back to you the soonest possible.</p>";
            _MSMailBody += "<p>Your have any inquiry please contact us email : ";
            _MSMailBody += "support@gluaygluay.com or call us at <strong>02-2688222</strong></p>";

            _MSMailBody += "</td></tr><tr><td align=\"left\">";
            _MSMailBody += "With best regards,</td></tr></table><hr><p>" +
            "<br/><strong><a href='http://www.gluaygluay.com'>GluayGluay</a> Support Team</strong><br/>" +
            "Comparexpress Insurance Broker ( Thailand ) Ltd,<br/>" +
            "License number  ว00016/2553<br/>" +
            "42nd Floor, United Center Building,<br/>" +
            "323 Silom Road, Bangrak, Bangkok 10500</p>" +
            "<p>Tel : 02-268-8222 , Fax : 02-266-6339<br/>" +
            "<a href=\"mailto:support@gluaygluay.com\">support@gluaygluay.com</a></p></td>" +
            "</tr></table><hr>";
        }

        DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();
        string YearEn = dateTimeNow.Year.ToString();
        strMailBody = new StringBuilder();
        strMailBody.Append(@"<table width='300' border='0' align='center' cellpadding='0' cellspacing='0'><tr>");
        strMailBody.Append("<td width='44' style='text-align: center'><a href='https://www.facebook.com/GGmotor'><img src='" + imgUrl + "images/icons/fb-big.png' width='44' height='42' alt=''/></a></td>");
        strMailBody.Append("<td width='44' style='text-align: center'><a href='https://plus.google.com/+Gluaygluay/posts'><img src='" + imgUrl + "images/icons/g-plus-big.png' width='44' height='42' alt=''/></a></td>");
        strMailBody.Append("<td width='44' style='text-align: center'><a href='https://www.youtube.com/channel/UCZ4gMtlXymBHxvcXO2cR1vA'><img src='" + imgUrl + "images/icons/youtube-big.png' width='44' height='42' alt=''/></a></td>");
        strMailBody.Append("<td width='44' style='text-align: center'><a href='http://iconosquare.com/ggmotor'><img src='" + imgUrl + "images/icons/instagram-big.png' width='44' height='42' alt=''/></a></td>");
        strMailBody.Append("<td width='43' style='text-align: center'><a href='https://www.linkedin.com/company/-com-gluaygluay-com-'><img src='" + imgUrl + "images/icons/linkedin-big.png' width='44' height='42' alt=''/></a></td>");
        strMailBody.Append(@"</tr>
                                         </table>
                                         <table width='800' height='100' border='0' cellspacing='0' cellpadding='0' background='images/email-footer.jpg'>
                                            <tr>
                                              <td><p style='text-align: center'><br><br>
                                                © " + YearEn + " gluaygluay.com. All rights reserved.<br>");
        if (!IsEN)
            strMailBody.Append("อีเมล์นี้ถูกส่งโดยระบบคอมพิวเตอร์ กรุณาอย่าตอบกลับอีเมล์นี้");
        else
            strMailBody.Append("This is a computer-generated email. Please do not reply to this email.");
        strMailBody.Append(@"</p></td></tr>
                                          </table></td>
                                          </tr>
                                        </table>
                                        </body>
                                        </html>");
        _MSMailBody += strMailBody.ToString();

        _MSToEmail = Email;
        _MSCCEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SUPPORT");

        _retValue = MyMailManager.SendGGMail(ConfigurationManager.AppSettings["GGServiceSMTPAccount"].ToString(), ConfigurationManager.AppSettings["GGServiceSMTPAuthCode"].ToString(), _MSToEmail, _MSCCEmail, _MSBCCEmail, _MSEmailTitle, _MSMailBody, Session[General.SMTPReplyTo].ToString());

        return _retValue;
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        grvApplication.DataSource = null;
        grvApplication.DataBind();
        GetFollowUpPilocy();
    }
}
